package com.sskj.quantization.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.quantization.bean.CashBeforeBean;
import com.sskj.quantization.bean.QuanRecordBean;
import com.sskj.quantization.http.HttpConfig;
import com.sskj.quantization.ui.fragment.RecordFragment;

import java.util.List;

import io.reactivex.Flowable;


public class RecordFragmentPresenter extends BasePresenter<RecordFragment> {


    /**
     * 确认兑换
     *
     * @param id   合约ID
     * @param tpwd 交易密码
     * @return
     */
    public void cash(String id, String tpwd) {
        httpService.cash(id, tpwd).execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {

                    mView.cashSuccess();
                }
            }
        });
    }

    /**
     * 兑换合约前页面
     *
     * @param id 合约ID
     * @return
     */
    public void cashBefore(String id) {
        httpService.cashBefore(id).execute(new JsonCallBack<HttpData<CashBeforeBean>>(this) {
            @Override
            public void onSuccess(Response<HttpData<CashBeforeBean>> response) {
                HttpData<CashBeforeBean> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {

                    mView.showCash(httpData.getData());
                }
            }
        });
    }

    /**
     * 量化记录
     *
     * @param type 1持有 2 完成
     * @param p    页码，默认1
     * @param size 页条数，默认5
     * @return
     */
    public Flowable<List<QuanRecordBean>> quanRecord(String type, String p, String size) {
        return httpService.quanRecord(type, p, size)
                .converter(new JsonConverter<HttpData<PageBean<QuanRecordBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());

    }
}
